<?php

//------------------------Functions for database------------------------------//
function create_database($database) {
    $database->database_query("CREATE TABLE IF NOT EXISTS
                `" . LINKS_TABLE . "`(
                `mid` INT AUTO_INCREMENT,
                `mid_key` VARCHAR( 5 ) NOT NULL,
                `link` VARCHAR( 100 ),
                `link_key` VARCHAR( 64 ) NOT NULL,
                PRIMARY KEY ( `mid`, `mid_key` ) )", "Unable to create table:");
}

function database_insert($database, $text, $id_key, $id = null) {
    $query = $database->database_query("INSERT INTO " . LINKS_TABLE .
                    "  ( mid, mid_key, link, link_key)
                VALUES ( '$id', '$id_key', '$text', '" .
                    hash("sha256", $text) . "')", "Unable to perform insertion query:");
    if (!query) { //return false if query error
        return false;
    }
}

function database_links($database) {
    $links = $database->database_query("SELECT COUNT(*) FROM " . LINKS_TABLE .
                    " ", "Unable to get number of pastes:");
    if (!$links) {
        return false;
    }
    $links = $database->database_fetch_array($links);
    return $links['COUNT(*)'] + 1; //Adding one as to signify the next unused num.
}

function database_retrieve($database, $id) {
    $paste = $database->database_query("SELECT * FROM " . LINKS_TABLE . "
                WHERE mid_key = '$id'", "Query error:");
    if (!$paste) { //If query didnt work return false
        return false;
    }
    $array = $database->database_fetch_assoc($paste);
    return $array;
}

function submit($post_paste, $dbname, $server, $user, $pass) {

    if (!get_magic_quotes_gpc()) {
        $text = addslashes($post_paste);
    } else {
        $text = $post_paste;
    }

    $db = new database($dbname, $server, $user, $pass);
    $db->database_connect();

    create_database($db);

    //Check if the paste already exists
    $paste = $db->database_query("SELECT mid_key FROM " . LINKS_TABLE .
                    " WHERE link_key =
            '" . hash("sha256", $text) . "'", "Error finding out if their are dublicates");
    if ($db->database_query_num_rows($paste)) {
        $array = $db->database_fetch_assoc($paste);
        return $array['mid_key'];
    }


    //Before ID creation, must check if paste already exists!
    $id = database_links($db);
    if (!id) {
        return false;
    }

    $key = make_hash();

    //Checks if their is already a copy of this key
    if ($db->database_query_num_rows(
                    $db->database_query("SELECT mid_key FROM " . LINKS_TABLE .
                            " WHERE mid_key =
            '$key'", "Error finding out if their are dublicates"))) {

        $key = make_hash();
        database_insert($db, $text, $key);
    } else {
        database_insert($db, $text, $key);
    }

    return $key;
}

function get_link($post_id, $dbname, $server, $user, $pass) {

    $db = new database($dbname, $server, $user, $pass);
    $db->database_connect();

    create_database($db);

    $array = database_retrieve($db, $post_id);
    $link = $array['link'];

    if (get_magic_quotes_gpc ()) {
        $link = stripslashes($link);
    }
    return $link;
}
?>
